package com.qf.myschool.servlet;

import cn.dsna.util.images.ValidateCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 生成验证码
 */
@WebServlet("/CheckCodeServlet")
public class CheckCodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//声明验证码的宽度
		int width=120;
		int height=40;
		//声明验证码字符个数
		int codeCount=4;
		//声明验证码干扰线条数
		int lineCount=10;
		//创建验证码对象
		ValidateCode code=new ValidateCode(width, height, codeCount, lineCount);
		
		//获得验证码字符
		String codeString=code.getCode();
		//获得session对象
		HttpSession session=request.getSession();
		//用session对象将生成验证码字符串存下来
		session.setAttribute("checkCode", codeString);
		
		//将验证码对象中内存图片通过网络输出流输出到客户端
		code.write(response.getOutputStream());
		
	}

}
